<?php
namespace lactone\Core;

use Lactone;

class Object{

    protected $Error = [];

    public function __construct($Config = []){

        \Lactone::initConfig($this,$Config);

        $this->init();
    }

    /**
     * 获取调用的类的名称
     * @return string
     */
    public function ClassName(){
        return get_called_class();
    }

    /**
     * 获取当前类配置项的key
     * @return mixed
     */
    public function getConfigName(){
        $Class = $this->ClassName();
        $R = explode("\\",$Class);
        return end($R);
    }

    public function init(){}

    /**
     * 日志
     * @param $data
     */
    public function log($data,$key = false,$level = 1){
       return false;
    }

    /**
     * 日志信息输出
     */
    public function logMessage($data,$key = false){
        if(!$key){
            $key = self::ClassName();
        }
        if(!is_string($data)){
            $data['_key'] = $key;
            return $data;
        }

        return "[$key] $data";
    }

    /**
     * 获取日志文件地址
     * @return string
     */
    public function getLogFile(){
        if( ($LogConfig = Lactone::getConfig('log')) && (isset($LogConfig['dir'])) ){
            $Dir = $LogConfig['dir'];
        }else{
            $Dir = __DIR__.'/';
        }

        return $Dir."log";
    }

    /**
     * 添加当前错误
     * @param $name
     * @param $value
     * @return mixed
     */
    public function addError($name,$value){
        return $this->Error[$name] = $value;
    }

    /**
     * 获取到了错误
     * @param $key
     * @return null
     */
    public function getError($key){
        if(isset($this->Error[$key])){
            return $this->Error[$key];
        }
        return NULL;
    }
}
?>